package cn.showw.uitl;

import java.util.*;

public class AixCookie {
	
	private Map<String, String> map = new HashMap<>();

	public AixCookie() {}

	public AixCookie(String cookies) {
		String[] strs = cookies.split(";");
		for (int i = 0; i < strs.length; i++) {
			String[] kv = strs[i].split("=");
			if (kv.length > 1)
				addCookie(kv[0].trim(), kv[1].trim());
		}
	}

	public int getSize() {
		return map.size();
	}

	public void addCookie(String cookie) {
		String[] strs = cookie.split(";");
		for (int i = 0; i < strs.length; i++) {
			String[] kv = strs[i].split("=");
			if (kv.length > 1)
				addCookie(kv[0].trim(), kv[1].trim());
		}
	}

	public void addCookie(String key, String value) {
		map.put(key, value);
	}

	public String getValue(String key) {
		return map.get(key);
	}

	public String toCookie() {
		StringBuffer cookie = new StringBuffer();
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			cookie.append(key + "=" + map.get(key) + "; ");
		}
		return cookie.toString();
	}
}
